package cn.edu.nit.sys.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * @Author 罗文斌
 * @Date 2023/4/15
 * @Version 1.0.0
 */
public class Utils {

    /**
     * 随机生成含大小写及数字的字符串
     * @return
     */
    public static String generateLettersAndNumbers() {
        int count = 10; // 生成字符的数量
        Random random = new Random();
        String str = "";
        for (int i = 0; i < count; i++) {
            int type = random.nextInt(3); // 随机生成字符的类型：0表示小写字母，1表示大写字母，2表示数字
            char c;
            switch (type) {
                case 0:
                    c = (char) (random.nextInt(26) + 'a');
                    break;
                case 1:
                    c = (char) (random.nextInt(26) + 'A');
                    break;
                default:
                    c = (char) (random.nextInt(10) + '0');
                    break;
            }
            str += c;
        }
        return str;
    }

    /**
     * 图片添加水印
     * @param path  图片路径
     * @param text  水印内容
     */
    public static void addWaterMark(String path, String text) {
        File file = new File(path);
        try {
            Image img = ImageIO.read(file);
            int imgWidth = img.getWidth(null);
            int imgHeight = img.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = bufferedImage.createGraphics();
            graphics.drawImage(img, 0, 0, imgWidth, imgHeight, null);
            graphics.setColor(new Color(255, 255, 255));
            graphics.setFont(new Font("微软雅黑", Font.BOLD, 42));
            graphics.drawString("@" + text,
                    imgWidth - getWatermarkLength("@" + text, graphics) - 10,
                    imgHeight - 20);
            graphics.dispose();
            FileOutputStream stream = new FileOutputStream(path);
            ImageIO.write(bufferedImage, "png", stream);
            stream.flush();
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    /**
     * 获取汉字拼音首字母
     * @param chinese   汉字
     * @return  字母
     */
    public static String getStrFirstLetter(String chinese) {
        int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274,
                2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
                4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 };
        String[] lc_FirstLetter = { "a", "b", "c", "d", "e",
                "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                "t", "w", "x", "y", "z" };
        chinese = Utils.conversionStr(chinese, "GB2312", "ISO8859-1");
        if (chinese.length() > 1) {     // 判断是不是汉字
            int li_SectorCode = (int) chinese.charAt(0); // 汉字区码
            int li_PositionCode = (int) chinese.charAt(1); // 汉字位码
            li_SectorCode = li_SectorCode - 160;
            li_PositionCode = li_PositionCode - 160;
            int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 汉字区位码
            if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {
                for (int i = 0; i < 23; i++) {
                    if (li_SecPosCode >= li_SecPosValue[i] && li_SecPosCode < li_SecPosValue[i + 1]) {
                        chinese = lc_FirstLetter[i];
                        break;
                    }
                }
            } else  {    // 非汉字字符,如图形符号或ASCII码
                chinese = Utils.conversionStr(chinese, "ISO8859-1", "GB2312");
                chinese = chinese.substring(0, 1);
            }
        }
        return chinese;
    }

    /**
     * 字符串编码转换
     * @param str 要转换编码的字符串
     * @param charsetName 原来的编码
     * @param toCharsetName 转换后的编码
     * @return 经过编码转换后的字符串
     */
    private static String conversionStr(String str, String charsetName,String toCharsetName) {
        try {
            str = new String(str.getBytes(charsetName), toCharsetName);
        } catch (UnsupportedEncodingException ex) {
            System.out.println("字符串编码转换异常：" + ex.getMessage());
        }
        return str;
    }
}
